Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a631887cd1 | ||
|
|
4ce8d266ab | ||
|
|
68474b56dc | ||
|
|
48faa0d4b1 | ||
|
|
9bf7c46ff6 | ||
|
|
195af14707 | ||
|
|
a9a492274c | ||
|
|
3cf82a7999 |
No files matched your search
@@ -1,11 +1,9 @@
|
|||||||
name: Publish Release
|
name: Publish Release
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
workflow_run:
|
||||||
tags:
|
workflows: ["Release"]
|
||||||
- 'v*'
|
types: [completed]
|
||||||
release:
|
|
||||||
types: [published]
|
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
inputs:
|
inputs:
|
||||||
dry_run:
|
dry_run:
|
||||||
@@ -20,7 +18,46 @@ on:
|
|||||||
default: false
|
default: false
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
check-release:
|
||||||
|
name: Check for new release
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
if: github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success'
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
outputs:
|
||||||
|
tag: ${{ steps.get_tag.outputs.tag }}
|
||||||
|
has_release: ${{ steps.get_tag.outputs.has_release }}
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Get latest release tag
|
||||||
|
id: get_tag
|
||||||
|
env:
|
||||||
|
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
run: |
|
||||||
|
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
||||||
|
version="$(node -e "process.stdout.write(require('./package.json').version)")"
|
||||||
|
tag="v$version"
|
||||||
|
echo "tag=$tag" >> "$GITHUB_OUTPUT"
|
||||||
|
echo "has_release=true" >> "$GITHUB_OUTPUT"
|
||||||
|
else
|
||||||
|
tag=$(gh release list --limit 1 --json tagName --jq '.[0].tagName // empty')
|
||||||
|
if [ -n "$tag" ]; then
|
||||||
|
echo "tag=$tag" >> "$GITHUB_OUTPUT"
|
||||||
|
echo "has_release=true" >> "$GITHUB_OUTPUT"
|
||||||
|
echo "Found release: $tag"
|
||||||
|
else
|
||||||
|
echo "has_release=false" >> "$GITHUB_OUTPUT"
|
||||||
|
echo "No release found, skipping build."
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
build:
|
build:
|
||||||
|
needs: check-release
|
||||||
|
if: needs.check-release.outputs.has_release == 'true'
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
@@ -136,16 +173,12 @@ jobs:
|
|||||||
echo "Dry run enabled: build artifacts generated and uploaded; release publish step will be skipped."
|
echo "Dry run enabled: build artifacts generated and uploaded; release publish step will be skipped."
|
||||||
|
|
||||||
publish:
|
publish:
|
||||||
name: Publish Release (Manual Approval)
|
name: Publish Release
|
||||||
needs: build
|
needs: [check-release, build]
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
if: needs.build.result == 'success' && (github.event_name != 'workflow_dispatch' || github.event.inputs.dry_run != 'true')
|
if: needs.build.result == 'success' && (github.event_name != 'workflow_dispatch' || github.event.inputs.dry_run != 'true')
|
||||||
permissions:
|
permissions:
|
||||||
contents: write
|
contents: write
|
||||||
# Configure the `release` environment with required reviewers in GitHub settings
|
|
||||||
# to enforce a manual approval gate before assets are uploaded.
|
|
||||||
environment:
|
|
||||||
name: release
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout git repo
|
- name: Checkout git repo
|
||||||
@@ -158,54 +191,17 @@ jobs:
|
|||||||
merge-multiple: true
|
merge-multiple: true
|
||||||
path: release-assets
|
path: release-assets
|
||||||
|
|
||||||
- name: Install pnpm
|
|
||||||
uses: pnpm/action-setup@v4
|
|
||||||
with:
|
|
||||||
version: 10
|
|
||||||
|
|
||||||
- name: Install Node and NPM
|
|
||||||
uses: actions/setup-node@v4
|
|
||||||
with:
|
|
||||||
node-version: 20
|
|
||||||
cache: 'pnpm'
|
|
||||||
|
|
||||||
- name: Resolve release tag
|
- name: Resolve release tag
|
||||||
id: release_tag
|
id: release_tag
|
||||||
env:
|
|
||||||
RELEASE_EVENT_TAG: ${{ github.event_name == 'release' && github.event.release.tag_name || '' }}
|
|
||||||
run: |
|
run: |
|
||||||
if [ -n "$RELEASE_EVENT_TAG" ]; then
|
tag="${{ needs.check-release.outputs.tag }}"
|
||||||
tag="$RELEASE_EVENT_TAG"
|
if [ -z "$tag" ]; then
|
||||||
else
|
version="$(node -e "process.stdout.write(require('./package.json').version)")"
|
||||||
version="$(node -e "const v=require('./package.json').version; if (!v) { throw new Error('package.json version is empty'); } process.stdout.write(v);")"
|
|
||||||
tag="v$version"
|
tag="v$version"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -z "$tag" ]; then
|
|
||||||
echo "::error::Resolved release tag is empty"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "tag=$tag" >> "$GITHUB_OUTPUT"
|
echo "tag=$tag" >> "$GITHUB_OUTPUT"
|
||||||
echo "Using release tag: $tag"
|
echo "Using release tag: $tag"
|
||||||
|
|
||||||
- name: Ensure release is safe to update
|
|
||||||
env:
|
|
||||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
RELEASE_TAG: ${{ steps.release_tag.outputs.tag }}
|
|
||||||
ALLOW_UPDATE_PUBLISHED_RELEASE: ${{ github.event_name == 'release' && 'true' || (github.event_name == 'workflow_dispatch' && github.event.inputs.allow_update_published_release) || 'false' }}
|
|
||||||
run: |
|
|
||||||
if gh release view "$RELEASE_TAG" >/dev/null 2>&1; then
|
|
||||||
is_draft="$(gh release view "$RELEASE_TAG" --json isDraft --jq '.isDraft')"
|
|
||||||
if [ "$is_draft" != "true" ] && [ "$ALLOW_UPDATE_PUBLISHED_RELEASE" != "true" ]; then
|
|
||||||
echo "::error::Release $RELEASE_TAG is already published. Refusing to upload assets."
|
|
||||||
echo "::error::Re-run with workflow_dispatch input allow_update_published_release=true if you explicitly want to update it."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
gh release create "$RELEASE_TAG" --title "$RELEASE_TAG" --draft --notes "Automated release draft."
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: Upload assets to release
|
- name: Upload assets to release
|
||||||
env:
|
env:
|
||||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|||||||
+1
-1
@@ -37,7 +37,7 @@
|
|||||||
"npmPublish": false
|
"npmPublish": false
|
||||||
}],
|
}],
|
||||||
["@semantic-release/git", {
|
["@semantic-release/git", {
|
||||||
"assets": ["package.json", "package-lock.json", "CHANGELOG.md"],
|
"assets": ["package.json", "pnpm-lock.yaml", "CHANGELOG.md"],
|
||||||
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
|
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
|
||||||
}],
|
}],
|
||||||
["@semantic-release/github", {
|
["@semantic-release/github", {
|
||||||
|
|||||||
@@ -1,3 +1,15 @@
|
|||||||
|
## [0.14.0](https://github.com/evandrodevbr/GeminiNexus/compare/v0.13.1...v0.14.0) (2026-04-29)
|
||||||
|
|
||||||
|
### 🚀 Features
|
||||||
|
|
||||||
|
* **proxy:** add OpenAI Responses API compatibility for Open CoDesign ([68474b5](https://github.com/evandrodevbr/GeminiNexus/commit/68474b56dca9c9bcd7b06eaae04c521f475e8e2c))
|
||||||
|
|
||||||
|
## [0.13.1](https://github.com/evandrodevbr/GeminiNexus/compare/v0.13.0...v0.13.1) (2026-04-29)
|
||||||
|
|
||||||
|
### 🐛 Bug Fixes
|
||||||
|
|
||||||
|
* lazy-initialize api-logger to prevent EACCES in CI environments ([9bf7c46](https://github.com/evandrodevbr/GeminiNexus/commit/9bf7c46ff6320f49cba80923c9e11eac35a7d4f0))
|
||||||
|
|
||||||
<a name="readme-top"></a>
|
<a name="readme-top"></a>
|
||||||
|
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "gemini-nexus",
|
"name": "gemini-nexus",
|
||||||
"productName": "Gemini Nexus",
|
"productName": "Gemini Nexus",
|
||||||
"version": "0.13.0",
|
"version": "0.14.0",
|
||||||
"description": "Gemini Nexus - Electron App",
|
"description": "Gemini Nexus - Electron App",
|
||||||
"main": ".vite/build/main.js",
|
"main": ".vite/build/main.js",
|
||||||
"private": true,
|
"private": true,
|
||||||
|
|||||||
@@ -162,6 +162,11 @@ function cleanJsonSchemaRecursive(value: any) {
|
|||||||
// 4. Physically remove "hard" blacklist items that interfere with generation
|
// 4. Physically remove "hard" blacklist items that interfere with generation
|
||||||
const hardRemoveFields = [
|
const hardRemoveFields = [
|
||||||
'$schema',
|
'$schema',
|
||||||
|
'$id',
|
||||||
|
'$comment',
|
||||||
|
'$anchor',
|
||||||
|
'$dynamicRef',
|
||||||
|
'$dynamicAnchor',
|
||||||
'additionalProperties',
|
'additionalProperties',
|
||||||
'enumCaseInsensitive',
|
'enumCaseInsensitive',
|
||||||
'enumNormalizeWhitespace',
|
'enumNormalizeWhitespace',
|
||||||
@@ -170,7 +175,15 @@ function cleanJsonSchemaRecursive(value: any) {
|
|||||||
'const',
|
'const',
|
||||||
'examples',
|
'examples',
|
||||||
// Advanced logic fields common in MCP tools but unsupported by Gemini
|
// Advanced logic fields common in MCP tools but unsupported by Gemini
|
||||||
|
'patternProperties',
|
||||||
'propertyNames',
|
'propertyNames',
|
||||||
|
'additionalItems',
|
||||||
|
'prefixItems',
|
||||||
|
'contains',
|
||||||
|
'minContains',
|
||||||
|
'maxContains',
|
||||||
|
'unevaluatedProperties',
|
||||||
|
'unevaluatedItems',
|
||||||
'anyOf',
|
'anyOf',
|
||||||
'oneOf',
|
'oneOf',
|
||||||
'allOf',
|
'allOf',
|
||||||
|
|||||||
@@ -8,17 +8,29 @@ import { sanitizeObject } from '../../utils/sensitiveDataMasking';
|
|||||||
const LOG_RETENTION = '30d';
|
const LOG_RETENTION = '30d';
|
||||||
const LOG_MAX_SIZE = '50m';
|
const LOG_MAX_SIZE = '50m';
|
||||||
|
|
||||||
const agentDir = getAgentDir();
|
let _logger: winston.Logger | null = null;
|
||||||
|
|
||||||
if (!fs.existsSync(agentDir)) {
|
/**
|
||||||
|
* Lazily initializes the API logger on first access.
|
||||||
|
* Avoids creating directories and file transports at module load time,
|
||||||
|
* preventing EACCES errors in CI/test environments.
|
||||||
|
*/
|
||||||
|
function getLogger(): winston.Logger {
|
||||||
|
if (_logger) {
|
||||||
|
return _logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
const agentDir = getAgentDir();
|
||||||
|
|
||||||
|
if (!fs.existsSync(agentDir)) {
|
||||||
try {
|
try {
|
||||||
fs.mkdirSync(agentDir, { recursive: true });
|
fs.mkdirSync(agentDir, { recursive: true });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Failed to create agent directory for API logs', e);
|
console.error('Failed to create agent directory for API logs', e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileFormat = winston.format.combine(
|
const fileFormat = winston.format.combine(
|
||||||
winston.format.timestamp(),
|
winston.format.timestamp(),
|
||||||
winston.format.printf(({ timestamp, level, message, ...meta }) => {
|
winston.format.printf(({ timestamp, level, message, ...meta }) => {
|
||||||
const logEntry = {
|
const logEntry = {
|
||||||
@@ -29,9 +41,9 @@ const fileFormat = winston.format.combine(
|
|||||||
};
|
};
|
||||||
return JSON.stringify(logEntry);
|
return JSON.stringify(logEntry);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
const consoleFormat = winston.format.combine(
|
const consoleFormat = winston.format.combine(
|
||||||
winston.format.colorize({ all: true }),
|
winston.format.colorize({ all: true }),
|
||||||
winston.format.printf(({ timestamp, level, message, ...meta }) => {
|
winston.format.printf(({ timestamp, level, message, ...meta }) => {
|
||||||
const logEntry = {
|
const logEntry = {
|
||||||
@@ -42,9 +54,9 @@ const consoleFormat = winston.format.combine(
|
|||||||
};
|
};
|
||||||
return JSON.stringify(logEntry);
|
return JSON.stringify(logEntry);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
const rotateTransport = new DailyRotateFile({
|
const rotateTransport = new DailyRotateFile({
|
||||||
filename: path.join(agentDir, 'api-%DATE%.log'),
|
filename: path.join(agentDir, 'api-%DATE%.log'),
|
||||||
datePattern: 'YYYY-MM-DD-HH',
|
datePattern: 'YYYY-MM-DD-HH',
|
||||||
maxSize: LOG_MAX_SIZE,
|
maxSize: LOG_MAX_SIZE,
|
||||||
@@ -53,25 +65,40 @@ const rotateTransport = new DailyRotateFile({
|
|||||||
auditFile: path.join(agentDir, '.api-log-audit.json'),
|
auditFile: path.join(agentDir, '.api-log-audit.json'),
|
||||||
level: 'debug',
|
level: 'debug',
|
||||||
format: fileFormat,
|
format: fileFormat,
|
||||||
});
|
});
|
||||||
|
|
||||||
rotateTransport.on('error', (error) => {
|
rotateTransport.on('error', (error) => {
|
||||||
console.error('DailyRotateFile API transport error', error);
|
console.error('DailyRotateFile API transport error', error);
|
||||||
});
|
});
|
||||||
|
|
||||||
const consoleTransport = new winston.transports.Console({
|
const consoleTransport = new winston.transports.Console({
|
||||||
level: 'debug',
|
level: 'debug',
|
||||||
format: consoleFormat,
|
format: consoleFormat,
|
||||||
});
|
});
|
||||||
|
|
||||||
consoleTransport.on('error', (error) => {
|
consoleTransport.on('error', (error) => {
|
||||||
console.error('Console API transport error', error);
|
console.error('Console API transport error', error);
|
||||||
});
|
});
|
||||||
|
|
||||||
export const apiLogger = winston.createLogger({
|
_logger = winston.createLogger({
|
||||||
level: 'debug',
|
level: 'debug',
|
||||||
transports: [consoleTransport, rotateTransport],
|
transports: [consoleTransport, rotateTransport],
|
||||||
exitOnError: false,
|
exitOnError: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
return _logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Lazily-initialized API logger instance. */
|
||||||
|
export const apiLogger = new Proxy({} as winston.Logger, {
|
||||||
|
get(_target, prop: string) {
|
||||||
|
const logger = getLogger();
|
||||||
|
const value = logger[prop as keyof winston.Logger];
|
||||||
|
if (typeof value === 'function') {
|
||||||
|
return value.bind(logger);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -46,7 +46,9 @@ export class ProxyController {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(ProxyService) private readonly proxyService: ProxyService,
|
@Inject(ProxyService) private readonly proxyService: ProxyService,
|
||||||
@Optional() @Inject(ProxyReplayService) private readonly proxyReplayService?: ProxyReplayService,
|
@Optional()
|
||||||
|
@Inject(ProxyReplayService)
|
||||||
|
private readonly proxyReplayService?: ProxyReplayService,
|
||||||
@Optional() @Inject(TokenManagerService) private readonly tokenManager?: TokenManagerService,
|
@Optional() @Inject(TokenManagerService) private readonly tokenManager?: TokenManagerService,
|
||||||
@Optional() @Inject(ProxyMetricsService) private readonly proxyMetrics?: ProxyMetricsService,
|
@Optional() @Inject(ProxyMetricsService) private readonly proxyMetrics?: ProxyMetricsService,
|
||||||
) {}
|
) {}
|
||||||
@@ -113,7 +115,9 @@ export class ProxyController {
|
|||||||
const metrics = this.proxyMetrics?.getMetrics();
|
const metrics = this.proxyMetrics?.getMetrics();
|
||||||
const circuitBreakerStatus = this.tokenManager?.getCircuitBreakerStatus() ?? {};
|
const circuitBreakerStatus = this.tokenManager?.getCircuitBreakerStatus() ?? {};
|
||||||
const accountCount = Object.keys(circuitBreakerStatus).length;
|
const accountCount = Object.keys(circuitBreakerStatus).length;
|
||||||
const healthyAccounts = Object.values(circuitBreakerStatus).filter(s => s.state === 'healthy').length;
|
const healthyAccounts = Object.values(circuitBreakerStatus).filter(
|
||||||
|
(s) => s.state === 'healthy',
|
||||||
|
).length;
|
||||||
|
|
||||||
res.status(HttpStatus.OK).send({
|
res.status(HttpStatus.OK).send({
|
||||||
status: 'healthy',
|
status: 'healthy',
|
||||||
@@ -162,7 +166,8 @@ export class ProxyController {
|
|||||||
}
|
}
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
try {
|
try {
|
||||||
const requests = this.proxyReplayService?.getRecentRequests(limit ? parseInt(limit, 10) : undefined) ?? [];
|
const requests =
|
||||||
|
this.proxyReplayService?.getRecentRequests(limit ? parseInt(limit, 10) : undefined) ?? [];
|
||||||
this.setProxyHeaders(res, { requestId: uuidv4(), accountId: '', retryCount: 0, model: '' });
|
this.setProxyHeaders(res, { requestId: uuidv4(), accountId: '', retryCount: 0, model: '' });
|
||||||
res.status(HttpStatus.OK).send({
|
res.status(HttpStatus.OK).send({
|
||||||
object: 'list',
|
object: 'list',
|
||||||
@@ -226,7 +231,8 @@ export class ProxyController {
|
|||||||
const capabilitiesMap = this.buildCapabilitiesMap();
|
const capabilitiesMap = this.buildCapabilitiesMap();
|
||||||
|
|
||||||
const data = modelIds.map((id) => {
|
const data = modelIds.map((id) => {
|
||||||
const baseId = Object.keys(capabilitiesMap).find((key) => id.includes(key)) ?? 'gemini-3-flash';
|
const baseId =
|
||||||
|
Object.keys(capabilitiesMap).find((key) => id.includes(key)) ?? 'gemini-3-flash';
|
||||||
return {
|
return {
|
||||||
id,
|
id,
|
||||||
object: 'model_capability',
|
object: 'model_capability',
|
||||||
@@ -247,7 +253,9 @@ export class ProxyController {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message = error instanceof Error ? error.message : 'Failed to list model capabilities';
|
const message = error instanceof Error ? error.message : 'Failed to list model capabilities';
|
||||||
this.logger.error(message, error instanceof Error ? error.stack : undefined);
|
this.logger.error(message, error instanceof Error ? error.stack : undefined);
|
||||||
res.status(HttpStatus.INTERNAL_SERVER_ERROR).send({ error: { message, type: 'internal_server_error' } });
|
res
|
||||||
|
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||||
|
.send({ error: { message, type: 'internal_server_error' } });
|
||||||
this.proxyMetrics?.recordRequest({
|
this.proxyMetrics?.recordRequest({
|
||||||
timestamp: startTime,
|
timestamp: startTime,
|
||||||
duration: Date.now() - startTime,
|
duration: Date.now() - startTime,
|
||||||
@@ -257,9 +265,27 @@ export class ProxyController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private buildCapabilitiesMap(): Record<string, { vision: boolean; streaming: boolean; jsonMode: boolean; audio: boolean; imageGeneration: boolean }> {
|
private buildCapabilitiesMap(): Record<
|
||||||
|
string,
|
||||||
|
{
|
||||||
|
vision: boolean;
|
||||||
|
streaming: boolean;
|
||||||
|
jsonMode: boolean;
|
||||||
|
audio: boolean;
|
||||||
|
imageGeneration: boolean;
|
||||||
|
}
|
||||||
|
> {
|
||||||
const allTokens = this.tokenManager?.getAllTokens() ?? [];
|
const allTokens = this.tokenManager?.getAllTokens() ?? [];
|
||||||
const map: Record<string, { vision: boolean; streaming: boolean; jsonMode: boolean; audio: boolean; imageGeneration: boolean }> = {};
|
const map: Record<
|
||||||
|
string,
|
||||||
|
{
|
||||||
|
vision: boolean;
|
||||||
|
streaming: boolean;
|
||||||
|
jsonMode: boolean;
|
||||||
|
audio: boolean;
|
||||||
|
imageGeneration: boolean;
|
||||||
|
}
|
||||||
|
> = {};
|
||||||
|
|
||||||
for (const [, tokenData] of allTokens) {
|
for (const [, tokenData] of allTokens) {
|
||||||
for (const [modelId, modelInfo] of Object.entries(tokenData.quota?.models ?? {})) {
|
for (const [modelId, modelInfo] of Object.entries(tokenData.quota?.models ?? {})) {
|
||||||
@@ -278,7 +304,13 @@ export class ProxyController {
|
|||||||
|
|
||||||
if (Object.keys(map).length === 0) {
|
if (Object.keys(map).length === 0) {
|
||||||
return {
|
return {
|
||||||
'gemini-3-flash': { vision: true, streaming: true, jsonMode: true, audio: false, imageGeneration: false },
|
'gemini-3-flash': {
|
||||||
|
vision: true,
|
||||||
|
streaming: true,
|
||||||
|
jsonMode: true,
|
||||||
|
audio: false,
|
||||||
|
imageGeneration: false,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -302,14 +334,14 @@ export class ProxyController {
|
|||||||
const request = requests[i];
|
const request = requests[i];
|
||||||
const proxyContext: ProxyRequestContext = {};
|
const proxyContext: ProxyRequestContext = {};
|
||||||
try {
|
try {
|
||||||
const result = await this.proxyService.handleChatCompletions(
|
const result = await this.proxyService.handleChatCompletions(request, proxyContext);
|
||||||
request,
|
|
||||||
proxyContext,
|
|
||||||
);
|
|
||||||
if (this.isObservableLike(result)) {
|
if (this.isObservableLike(result)) {
|
||||||
responses.push({
|
responses.push({
|
||||||
index: i,
|
index: i,
|
||||||
error: { message: 'Streaming not supported in batch mode', type: 'invalid_request_error' },
|
error: {
|
||||||
|
message: 'Streaming not supported in batch mode',
|
||||||
|
type: 'invalid_request_error',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
responses.push({
|
responses.push({
|
||||||
@@ -462,7 +494,14 @@ export class ProxyController {
|
|||||||
const result = await this.proxyService.handleChatCompletions(request, proxyContext);
|
const result = await this.proxyService.handleChatCompletions(request, proxyContext);
|
||||||
isStream = !!(body.stream && this.isObservableLike(result));
|
isStream = !!(body.stream && this.isObservableLike(result));
|
||||||
if (isStream) {
|
if (isStream) {
|
||||||
this.writeSseResponse(res, result as Observable<unknown>, undefined, '/v1/completions', startTime, proxyContext);
|
this.writeSseResponse(
|
||||||
|
res,
|
||||||
|
result as Observable<unknown>,
|
||||||
|
undefined,
|
||||||
|
'/v1/completions',
|
||||||
|
startTime,
|
||||||
|
proxyContext,
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -508,6 +547,7 @@ export class ProxyController {
|
|||||||
temperature?: number;
|
temperature?: number;
|
||||||
top_p?: number;
|
top_p?: number;
|
||||||
stream?: boolean;
|
stream?: boolean;
|
||||||
|
reasoning?: { effort?: string };
|
||||||
},
|
},
|
||||||
@Req() req: FastifyRequest,
|
@Req() req: FastifyRequest,
|
||||||
@Res() res: FastifyReply,
|
@Res() res: FastifyReply,
|
||||||
@@ -527,11 +567,24 @@ export class ProxyController {
|
|||||||
const proxyContext: ProxyRequestContext = {};
|
const proxyContext: ProxyRequestContext = {};
|
||||||
let isStream = false;
|
let isStream = false;
|
||||||
|
|
||||||
|
this.logger.log(
|
||||||
|
`[/v1/responses] Converted input: ${Array.isArray(body.input) ? body.input.length : 0} items → ${request.messages.length} messages, ` +
|
||||||
|
`model=${request.model}, stream=${body.stream}, tools=${request.tools?.length ?? 0}, ` +
|
||||||
|
`reasoning=${body.reasoning?.effort ?? 'default'}`,
|
||||||
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await this.proxyService.handleChatCompletions(request, proxyContext);
|
const result = await this.proxyService.handleChatCompletions(request, proxyContext);
|
||||||
isStream = !!(body.stream && this.isObservableLike(result));
|
isStream = !!(body.stream && this.isObservableLike(result));
|
||||||
if (isStream) {
|
if (isStream) {
|
||||||
this.writeSseResponse(res, result as Observable<unknown>, undefined, '/v1/responses', startTime, proxyContext);
|
this.writeSseResponse(
|
||||||
|
res,
|
||||||
|
result as Observable<unknown>,
|
||||||
|
undefined,
|
||||||
|
'/v1/responses',
|
||||||
|
startTime,
|
||||||
|
proxyContext,
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -840,14 +893,18 @@ export class ProxyController {
|
|||||||
const proxyContext: ProxyRequestContext = {};
|
const proxyContext: ProxyRequestContext = {};
|
||||||
let isStream = false;
|
let isStream = false;
|
||||||
try {
|
try {
|
||||||
const result = await this.proxyService.handleChatCompletions(
|
const result = await this.proxyService.handleChatCompletions(body, proxyContext);
|
||||||
body,
|
|
||||||
proxyContext,
|
|
||||||
);
|
|
||||||
|
|
||||||
isStream = !!(body.stream && this.isObservableLike(result));
|
isStream = !!(body.stream && this.isObservableLike(result));
|
||||||
if (isStream) {
|
if (isStream) {
|
||||||
this.writeSseResponse(res, result as Observable<unknown>, requestId, '/v1/chat/completions', startTime, proxyContext);
|
this.writeSseResponse(
|
||||||
|
res,
|
||||||
|
result as Observable<unknown>,
|
||||||
|
requestId,
|
||||||
|
'/v1/chat/completions',
|
||||||
|
startTime,
|
||||||
|
proxyContext,
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
const duration = Date.now() - startTime;
|
const duration = Date.now() - startTime;
|
||||||
@@ -918,7 +975,14 @@ export class ProxyController {
|
|||||||
isStream = !!(body.stream && this.isObservableLike(result));
|
isStream = !!(body.stream && this.isObservableLike(result));
|
||||||
|
|
||||||
if (isStream) {
|
if (isStream) {
|
||||||
this.writeSseResponse(res, result as Observable<unknown>, undefined, '/v1/messages', startTime, proxyContext);
|
this.writeSseResponse(
|
||||||
|
res,
|
||||||
|
result as Observable<unknown>,
|
||||||
|
undefined,
|
||||||
|
'/v1/messages',
|
||||||
|
startTime,
|
||||||
|
proxyContext,
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
const duration = Date.now() - startTime;
|
const duration = Date.now() - startTime;
|
||||||
@@ -1017,6 +1081,7 @@ export class ProxyController {
|
|||||||
temperature?: number;
|
temperature?: number;
|
||||||
top_p?: number;
|
top_p?: number;
|
||||||
stream?: boolean;
|
stream?: boolean;
|
||||||
|
reasoning?: { effort?: string };
|
||||||
}): OpenAIChatRequest {
|
}): OpenAIChatRequest {
|
||||||
const messages: OpenAIChatRequest['messages'] = [];
|
const messages: OpenAIChatRequest['messages'] = [];
|
||||||
if (isString(body.instructions) && !isEmpty(body.instructions.trim())) {
|
if (isString(body.instructions) && !isEmpty(body.instructions.trim())) {
|
||||||
@@ -1054,21 +1119,59 @@ export class ProxyController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const item of inputItems) {
|
for (let idx = 0; idx < inputItems.length; idx++) {
|
||||||
|
const item = inputItems[idx];
|
||||||
const itemObj = this.toRecord(item);
|
const itemObj = this.toRecord(item);
|
||||||
if (!itemObj) {
|
if (!itemObj) {
|
||||||
|
this.logger.debug(`[/v1/responses] input[${idx}]: skipped (not an object)`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const type = this.asString(itemObj.type);
|
const type = this.asString(itemObj.type);
|
||||||
|
const role = this.asString(itemObj.role);
|
||||||
|
|
||||||
|
// Items without a type but with role/content — treat as regular chat messages
|
||||||
|
// (common in clients like Open CoDesign that mix chat and responses formats)
|
||||||
if (!type) {
|
if (!type) {
|
||||||
|
if (role && itemObj.content !== undefined) {
|
||||||
|
const content = this.normalizeResponsesMessageContent(itemObj.content);
|
||||||
|
messages.push({ role, content });
|
||||||
|
this.logger.debug(`[/v1/responses] input[${idx}]: no-type fallback → role=${role}`);
|
||||||
|
} else {
|
||||||
|
this.logger.warn(
|
||||||
|
`[/v1/responses] input[${idx}]: dropped (no type, no role/content). Keys: ${Object.keys(itemObj).join(',')}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'message') {
|
if (type === 'message') {
|
||||||
const role = this.asString(itemObj.role) ?? 'user';
|
const messageRole = role ?? 'user';
|
||||||
const content = this.normalizeResponsesMessageContent(itemObj.content);
|
const content = this.normalizeResponsesMessageContent(itemObj.content);
|
||||||
messages.push({ role, content });
|
messages.push({ role: messageRole, content });
|
||||||
|
this.logger.debug(`[/v1/responses] input[${idx}]: type=message, role=${messageRole}`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Responses API text item types
|
||||||
|
if (type === 'input_text') {
|
||||||
|
const text = this.asString(itemObj.text) ?? '';
|
||||||
|
if (text) {
|
||||||
|
messages.push({ role: 'user', content: text });
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type === 'output_text') {
|
||||||
|
const text = this.asString(itemObj.text) ?? '';
|
||||||
|
if (text) {
|
||||||
|
messages.push({ role: 'assistant', content: text });
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reasoning items can be silently skipped — they are model-internal
|
||||||
|
if (type === 'reasoning') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1105,6 +1208,13 @@ export class ProxyController {
|
|||||||
});
|
});
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fallback: if the item has role + content but an unrecognized type, treat as message
|
||||||
|
const fallbackRole = this.asString(itemObj.role);
|
||||||
|
if (fallbackRole && itemObj.content !== undefined) {
|
||||||
|
const content = this.normalizeResponsesMessageContent(itemObj.content);
|
||||||
|
messages.push({ role: fallbackRole, content });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (isString(body.input)) {
|
} else if (isString(body.input)) {
|
||||||
messages.push({
|
messages.push({
|
||||||
@@ -1125,17 +1235,64 @@ export class ProxyController {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Forward reasoning effort to the extra field so convertOpenAIToClaude can relay it
|
||||||
|
const extra: Record<string, unknown> = {};
|
||||||
|
if (body.reasoning?.effort) {
|
||||||
|
extra.reasoning_effort = body.reasoning.effort;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normalize Responses API tool format to Chat Completions format.
|
||||||
|
// Responses API: { type: "function", name: "...", parameters: {...} }
|
||||||
|
// Chat Completions: { type: "function", function: { name: "...", parameters: {...} } }
|
||||||
|
const normalizedTools = this.normalizeResponsesTools(body.tools);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
model: body.model ?? 'gemini-3-flash',
|
model: body.model ?? 'gemini-3-flash',
|
||||||
messages,
|
messages,
|
||||||
tools: body.tools,
|
tools: normalizedTools,
|
||||||
max_tokens: body.max_output_tokens,
|
max_tokens: body.max_output_tokens,
|
||||||
temperature: body.temperature,
|
temperature: body.temperature,
|
||||||
top_p: body.top_p,
|
top_p: body.top_p,
|
||||||
stream: body.stream,
|
stream: body.stream,
|
||||||
|
...(Object.keys(extra).length > 0 ? { extra } : {}),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert tools from OpenAI Responses API format to Chat Completions format.
|
||||||
|
* Responses API puts name/description/parameters at the top level of the tool object,
|
||||||
|
* while Chat Completions nests them under a `.function` property.
|
||||||
|
*/
|
||||||
|
private normalizeResponsesTools(tools?: OpenAIChatRequest['tools']): OpenAIChatRequest['tools'] {
|
||||||
|
if (!tools || tools.length === 0) {
|
||||||
|
return tools;
|
||||||
|
}
|
||||||
|
|
||||||
|
return tools.map((tool) => {
|
||||||
|
// Already in Chat Completions format (has .function.name)
|
||||||
|
if (tool.function?.name) {
|
||||||
|
return tool;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Responses API format: name/description/parameters at top level
|
||||||
|
const topLevelName = this.asString((tool as Record<string, unknown>).name);
|
||||||
|
if (topLevelName) {
|
||||||
|
const raw = tool as Record<string, unknown>;
|
||||||
|
return {
|
||||||
|
type: 'function',
|
||||||
|
function: {
|
||||||
|
name: topLevelName,
|
||||||
|
description: this.asString(raw.description) ?? undefined,
|
||||||
|
parameters: (raw.parameters ?? raw.input_schema) as Record<string, unknown> | undefined,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unknown format — pass through as-is
|
||||||
|
return tool;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private normalizeResponsesMessageContent(content: unknown): string | OpenAIContentPart[] {
|
private normalizeResponsesMessageContent(content: unknown): string | OpenAIContentPart[] {
|
||||||
if (isString(content)) {
|
if (isString(content)) {
|
||||||
return content;
|
return content;
|
||||||
|
|||||||
@@ -55,7 +55,10 @@ export interface ProxyConfigOverride {
|
|||||||
@Injectable()
|
@Injectable()
|
||||||
export class ProxyService {
|
export class ProxyService {
|
||||||
private readonly logger = new Logger(ProxyService.name);
|
private readonly logger = new Logger(ProxyService.name);
|
||||||
private readonly responseCache = new Map<string, { response: OpenAIChatResponse; expiry: number }>();
|
private readonly responseCache = new Map<
|
||||||
|
string,
|
||||||
|
{ response: OpenAIChatResponse; expiry: number }
|
||||||
|
>();
|
||||||
private readonly CACHE_TTL_MS = 60000; // 1 minute
|
private readonly CACHE_TTL_MS = 60000; // 1 minute
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@@ -79,7 +82,7 @@ export class ProxyService {
|
|||||||
}
|
}
|
||||||
const prompt = usage.promptTokenCount ?? 0;
|
const prompt = usage.promptTokenCount ?? 0;
|
||||||
const completion = usage.candidatesTokenCount ?? 0;
|
const completion = usage.candidatesTokenCount ?? 0;
|
||||||
const total = usage.totalTokenCount ?? (prompt + completion);
|
const total = usage.totalTokenCount ?? prompt + completion;
|
||||||
this.tokenUsageService.recordUsage({
|
this.tokenUsageService.recordUsage({
|
||||||
accountId,
|
accountId,
|
||||||
model,
|
model,
|
||||||
@@ -995,7 +998,10 @@ export class ProxyService {
|
|||||||
`Transformed Claude response snippet: ${JSON.stringify(claudeResponse).substring(0, 500)}`,
|
`Transformed Claude response snippet: ${JSON.stringify(claudeResponse).substring(0, 500)}`,
|
||||||
);
|
);
|
||||||
const openaiResponse = this.convertClaudeToOpenAIResponse(claudeResponse, request.model);
|
const openaiResponse = this.convertClaudeToOpenAIResponse(claudeResponse, request.model);
|
||||||
const transformedResponse = this.applyResponseFormatTransformation(openaiResponse, request);
|
const transformedResponse = this.applyResponseFormatTransformation(
|
||||||
|
openaiResponse,
|
||||||
|
request,
|
||||||
|
);
|
||||||
this.setCachedResponse(this.buildCacheKey(request), transformedResponse);
|
this.setCachedResponse(this.buildCacheKey(request), transformedResponse);
|
||||||
if (context) {
|
if (context) {
|
||||||
context.cacheStatus = 'MISS';
|
context.cacheStatus = 'MISS';
|
||||||
@@ -1058,8 +1064,14 @@ export class ProxyService {
|
|||||||
);
|
);
|
||||||
this.recordUsage(token.id, effectiveTargetModel, response.usageMetadata, 'openai');
|
this.recordUsage(token.id, effectiveTargetModel, response.usageMetadata, 'openai');
|
||||||
const claudeResponse = transformResponse(response);
|
const claudeResponse = transformResponse(response);
|
||||||
const openaiResponse = this.convertClaudeToOpenAIResponse(claudeResponse, request.model);
|
const openaiResponse = this.convertClaudeToOpenAIResponse(
|
||||||
const transformedResponse = this.applyResponseFormatTransformation(openaiResponse, request);
|
claudeResponse,
|
||||||
|
request.model,
|
||||||
|
);
|
||||||
|
const transformedResponse = this.applyResponseFormatTransformation(
|
||||||
|
openaiResponse,
|
||||||
|
request,
|
||||||
|
);
|
||||||
this.setCachedResponse(this.buildCacheKey(request), transformedResponse);
|
this.setCachedResponse(this.buildCacheKey(request), transformedResponse);
|
||||||
if (context) {
|
if (context) {
|
||||||
context.cacheStatus = 'MISS';
|
context.cacheStatus = 'MISS';
|
||||||
@@ -1636,7 +1648,8 @@ export class ProxyService {
|
|||||||
const anthropicMessages: ClaudeRequest['messages'] = [];
|
const anthropicMessages: ClaudeRequest['messages'] = [];
|
||||||
|
|
||||||
for (const msg of messages) {
|
for (const msg of messages) {
|
||||||
if (msg.role === 'system') {
|
// 'developer' is OpenAI's newer equivalent of 'system'
|
||||||
|
if (msg.role === 'system' || msg.role === 'developer') {
|
||||||
const systemText = this.extractOpenAITextContent(msg.content);
|
const systemText = this.extractOpenAITextContent(msg.content);
|
||||||
if (systemText) {
|
if (systemText) {
|
||||||
systemPromptParts.push(systemText);
|
systemPromptParts.push(systemText);
|
||||||
@@ -1679,12 +1692,31 @@ export class ProxyService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.response_format?.type === 'json_object' || request.response_format?.type === 'json_schema') {
|
if (
|
||||||
systemPromptParts.push('You must format your entire response as a single valid JSON object. Do not include any markdown formatting, explanations, or text outside the JSON structure.');
|
request.response_format?.type === 'json_object' ||
|
||||||
|
request.response_format?.type === 'json_schema'
|
||||||
|
) {
|
||||||
|
systemPromptParts.push(
|
||||||
|
'You must format your entire response as a single valid JSON object. Do not include any markdown formatting, explanations, or text outside the JSON structure.',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const systemPrompt = systemPromptParts.length > 0 ? systemPromptParts.join('\n') : undefined;
|
const systemPrompt = systemPromptParts.length > 0 ? systemPromptParts.join('\n') : undefined;
|
||||||
|
|
||||||
|
// Map OpenAI reasoning_effort to Claude thinking config
|
||||||
|
const reasoningEffort = String(request.extra?.reasoning_effort ?? '').toLowerCase();
|
||||||
|
let thinking: ClaudeRequest['thinking'] | undefined;
|
||||||
|
if (reasoningEffort === 'none') {
|
||||||
|
// Explicitly disable thinking when client requests no reasoning
|
||||||
|
thinking = { type: 'disabled' };
|
||||||
|
} else if (
|
||||||
|
reasoningEffort === 'low' ||
|
||||||
|
reasoningEffort === 'medium' ||
|
||||||
|
reasoningEffort === 'high'
|
||||||
|
) {
|
||||||
|
thinking = { type: 'adaptive', effort: reasoningEffort };
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
model: request.model,
|
model: request.model,
|
||||||
messages: anthropicMessages,
|
messages: anthropicMessages,
|
||||||
@@ -1694,6 +1726,7 @@ export class ProxyService {
|
|||||||
temperature: request.temperature,
|
temperature: request.temperature,
|
||||||
top_p: request.top_p,
|
top_p: request.top_p,
|
||||||
stream: request.stream,
|
stream: request.stream,
|
||||||
|
...(thinking ? { thinking } : {}),
|
||||||
metadata: {
|
metadata: {
|
||||||
...(request.extra ?? {}),
|
...(request.extra ?? {}),
|
||||||
source: 'openai',
|
source: 'openai',
|
||||||
@@ -1704,10 +1737,19 @@ export class ProxyService {
|
|||||||
private convertOpenAIPartsToAnthropicContent(
|
private convertOpenAIPartsToAnthropicContent(
|
||||||
content: OpenAIChatRequest['messages'][number]['content'],
|
content: OpenAIChatRequest['messages'][number]['content'],
|
||||||
): AnthropicContent[] {
|
): AnthropicContent[] {
|
||||||
|
if (content == null) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
if (isString(content)) {
|
if (isString(content)) {
|
||||||
return content.trim() ? [{ type: 'text', text: content }] : [];
|
return content.trim() ? [{ type: 'text', text: content }] : [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Guard against non-array content (e.g. an object or number)
|
||||||
|
if (!Array.isArray(content)) {
|
||||||
|
return [{ type: 'text', text: String(content) }];
|
||||||
|
}
|
||||||
|
|
||||||
const blocks: AnthropicContent[] = [];
|
const blocks: AnthropicContent[] = [];
|
||||||
for (const part of content) {
|
for (const part of content) {
|
||||||
if (part.type === 'text' && part.text) {
|
if (part.type === 'text' && part.text) {
|
||||||
@@ -1738,10 +1780,18 @@ export class ProxyService {
|
|||||||
private extractOpenAITextContent(
|
private extractOpenAITextContent(
|
||||||
content: OpenAIChatRequest['messages'][number]['content'],
|
content: OpenAIChatRequest['messages'][number]['content'],
|
||||||
): string {
|
): string {
|
||||||
|
if (content == null) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
if (isString(content)) {
|
if (isString(content)) {
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!Array.isArray(content)) {
|
||||||
|
return String(content);
|
||||||
|
}
|
||||||
|
|
||||||
return content
|
return content
|
||||||
.filter((part) => part.type === 'text')
|
.filter((part) => part.type === 'text')
|
||||||
.map((part) => part.text || '')
|
.map((part) => part.text || '')
|
||||||
|
|||||||
Reference in new issue
Block a user